from das.stock_tag_das import StockTagDas, StockTagModel
from plugins.ant_das.base_dao import QueryCondition
from typing import List


def create_stock_tag() -> int:
    """
    创建一个新的股票标签
    :return: 新创建标签的ID
    """
    stock_tag_das = StockTagDas()
    
    # 先检查标签是否已存在
    existing_tag = stock_tag_das.query_by_code("tag_code", "NEW_TEST_TAG")
    if existing_tag:
        print(f"标签已存在，ID: {existing_tag.id}")
        return existing_tag.id

    new_tag = StockTagModel(
        tag_name="测试标签",
        tag_code="NEW_TEST_TAG",
        tag_group="测试分组",
        tag_status=1,
        tag_weight=1.0,
        tag_ratio=0.5,
    )

    tag_id = stock_tag_das.insert_data(new_tag)
    print(f"插入的标签ID: {tag_id}")
    return tag_id


def query_by_java_like_syntax():
    """使用类似Java的语法进行查询"""
    stock_tag_das = StockTagDas()

    # 获取字段引用工厂
    fields = stock_tag_das.fields

    # 创建查询条件
    condition = stock_tag_das.create_query_condition()

    # 使用Java风格的方法引用添加条件
    # 类似于: dataMapSearchWrapper.eq(ConfigDataMapEntity::getTagCode, "GROWTH");
    condition.eq(fields.getTagCode, "GROWTH")
    condition.eq(fields.getTagGroup, "基本面")

    # 执行查询
    tag = stock_tag_das.query_one(condition)
    print(f"使用Java风格语法查询结果: {tag}")

    return tag


def query_by_complex_condition():
    """使用复合条件查询示例"""
    stock_tag_das = StockTagDas()
    fields = stock_tag_das.fields

    # 链式调用示例
    # 类似于: dataMapSearchWrapper.eq(ConfigDataMapEntity::getTagGroup, "基本面").gt(ConfigDataMapEntity::getTagWeight, 1.0);
    condition = stock_tag_das.create_query_condition()
    condition.eq(fields.getTagGroup, "基本面").gt(fields.getTagWeight, 1.0)

    tags = stock_tag_das.query(condition)
    print(f"链式调用查询结果数量: {len(tags)}")
    return tags


def query_by_in_condition():
    """使用IN条件查询示例"""
    stock_tag_das = StockTagDas()
    fields = stock_tag_das.fields

    # IN条件示例
    # 类似于: dataMapSearchWrapper.in(ConfigDataMapEntity::getTagCode, codeList);
    condition = stock_tag_das.create_query_condition()
    codes = ["GROWTH", "VALUE", "MOMENTUM"]
    condition.in_(fields.getTagCode, codes)

    tags = stock_tag_das.query(condition)
    print(f"IN条件查询结果数量: {len(tags)}")
    return tags


def query_by_multiple_conditions():
    """使用多个条件组合查询示例"""
    stock_tag_das = StockTagDas()
    fields = stock_tag_das.fields

    # 多条件组合查询示例
    condition = stock_tag_das.create_query_condition()
    condition.eq(fields.getTagGroup, "基本面")
    condition.gt(fields.getTagWeight, 1.0)
    condition.ne(fields.getTagStatus, 0)

    tags = stock_tag_das.query(condition)
    print(f"多条件组合查询结果数量: {len(tags)}")
    return tags


def query_by_convenience_method():
    """使用便捷方法查询示例"""
    stock_tag_das = StockTagDas()

    # 按代码查询单个记录
    tag = stock_tag_das.query_by_code("tag_code", "GROWTH")
    print(f"按代码查询结果: {tag}")

    # 按分组查询多个记录
    tags_by_group = stock_tag_das.query_by_group("tag_group", "基本面")
    print(f"按分组查询到的标签数量: {len(tags_by_group)}")

    # 按多个代码查询
    codes = ["GROWTH", "VALUE", "MOMENTUM"]
    tags_by_codes = stock_tag_das.query_by_codes("tag_code", codes)
    print(f"按多个代码查询到的标签数量: {len(tags_by_codes)}")

    return tags_by_group


def update_stock_tag(tag: StockTagModel):
    """
    更新股票标签
    :param tag: 要更新的标签
    """
    stock_tag_das = StockTagDas()

    if tag:
        tag.tag_weight = 2.0
        stock_tag_das.update_data(tag)
        print("更新标签权重成功")


def delete_stock_tag(tag_id: int):
    """
    删除股票标签
    :param tag_id: 要删除的标签ID
    """
    stock_tag_das = StockTagDas()

    # 检查是否是我们自己创建的测试标签
    tag = stock_tag_das.query_by_code("tag_code", "NEW_TEST_TAG")
    if tag and tag.id == tag_id:
        deleted = stock_tag_das.delete_by_id(tag_id)
        print(f"删除标签结果: {'成功' if deleted else '失败'}")
    else:
        print("跳过删除操作，只删除测试标签")


def run_example():
    """运行示例，可以从外部调用"""
    print("\n=== 开始运行StockTagDas查询示例 ===\n")
    
    # 创建标签
    tag_id = create_stock_tag()

    # 使用类似Java的语法查询
    tag = query_by_java_like_syntax()

    # 使用复合条件查询
    query_by_complex_condition()

    # 使用IN条件查询
    query_by_in_condition()

    # 使用多条件组合查询
    query_by_multiple_conditions()

    # 使用便捷方法查询
    query_by_convenience_method()

    # 更新标签
    update_stock_tag(tag)

    # 删除标签
    delete_stock_tag(tag_id)
    
    print("\n=== StockTagDas查询示例运行完成 ===\n")


# 如果直接运行此脚本，则执行示例
if __name__ == "__main__":
    run_example()